package com.edu.eduservice.controller;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.edu.common.utils.R;
import com.edu.eduservice.client.VodClient;
import com.edu.eduservice.entity.EduVideo;
import com.edu.eduservice.service.EduChapterService;
import com.edu.eduservice.service.EduCourseService;
import com.edu.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-30
 */
@Api(description = "小节管理")
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    //小节
    @Autowired
    private EduVideoService eduVideoService;
    //课程
    @Autowired
    private EduCourseService courseService;
    //章节
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private VodClient vodClient;


    /**
     * 添加小节
     */
    @ApiOperation(value = "添加小节")
    @PostMapping("/addVideo")
    @Transactional
    public R addVideo(@ApiParam(value = "eduVideo",name = "添加小节",required = true)
                    @RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }



    /**
     * 删除小节
     *
     */
    @ApiOperation(value = "根据id删除小节")
    @DeleteMapping("/{id}")
    @Transactional
    public R delVideo(@ApiParam(value = "id",name = "删除小节",required = true)
                    @PathVariable("id") String id){

        EduVideo eduVideo = eduVideoService.getById(id);

        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id 远程调用删除视频
            vodClient.delVideo(videoSourceId);
        }

        //删除小节
        boolean b = eduVideoService.removeById(id);

        if (b){
            return R.ok();
        }else {
            return R.error().message("删除失败!");
        }

}

    /**
     * 修改小节
     */
        @ApiOperation(value = "修改小节")
        @PostMapping("/updateVideo")
        @Transactional
    public R updateVideo(@ApiParam(value = "eduVideo",name = "修改小节",required = true)
                        @RequestBody EduVideo eduVideo){
        boolean b = eduVideoService.updateById(eduVideo);
        if (b){
           return R.ok();
        }else {
            return R.error().message("修改失败!");
        }

    }


    /**
     * 根据id查询小节
     */
    @ApiOperation(value = "根据id查询小节")
    @PostMapping("/queryVideo/{videoId}")
    @Transactional
    public R queryVideo(@ApiParam(value = "videoId",name = "查询小节",required = true)
                    @PathVariable("videoId") String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);

        return R.ok().data("video",eduVideo);
    }

}

